package com.yksj.healthtalk.ui.messageleave;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 客户端-留言
 * @author Administrator
 *
 */
public class DoctorMessageBoardLeaveMessage extends BaseFragmentActivity implements OnClickListener {
	private EditText mEditText;
	private TextView textNum;
	private final int TEXTCOUNT=30;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_leave_message);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		initView();
	}
	
	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleTextV.setText("留言");
		
		mEditText = (EditText)findViewById(R.id.setting_feedback_text);
		mEditText.addTextChangedListener(textWatcher);
		textNum = (TextView)findViewById(R.id.textcount);
		textNum.setText("0/"+TEXTCOUNT);
		
	}

	/**
	 * 文字监听
	 */
	private TextWatcher textWatcher = new TextWatcher() {  
        @Override    
        public void afterTextChanged(Editable s) { 
        }   
        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count,  
                int after) {  
        }  
         @Override    
        public void onTextChanged(CharSequence s, int start, int before,     
                int count) {   
        	 if((start +count)<=TEXTCOUNT && mEditText.getText().toString().length()<=TEXTCOUNT){
        		int textNumber = mEditText.getText().toString().length();
        		 textNum.setText(textNumber+"/"+TEXTCOUNT); 
        	 }else{
        		 mEditText.setText(s.subSequence(0, TEXTCOUNT));
        		 ToastUtil.showShort(DoctorMessageBoardLeaveMessage.this, "最多可输入"+TEXTCOUNT+"个字符");
        	 }
        }                    
    };
    
	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.title_back:
				SystemUtils.hideSoftBord(this,mEditText);
				setResult(RESULT_OK, getIntent());
				finish();
				overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
				break;
			case R.id.title_right2:
				if(mEditText.getText().toString().length()>0){
					submitFeedback();
				}
				break;
			}
		}
	
	/**
	 * 提交留言
	 */
	private void submitFeedback(){
		HttpRestClient.doHttpLeaveMessage(getIntent().getStringExtra("id"),mEditText.getText().toString(),new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				//{"info":"留言条数已经存在两条","code":"2"}
				try {
					JSONObject object=new JSONObject(content);
					String str=object.optString("code");
					String con=object.optString("info");
					if(str.equals("0")){
						Intent intent = getIntent();
						intent.putExtra("tishi", con);
						setResult(Activity.RESULT_OK, intent);
						finish();
						overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
					}else{
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),con);
					}
				} catch (JSONException e) {
				}
			}
		});
	}
}
